package cn.elricyu.tt.action.officer;

import java.util.List;

import net.sf.json.JSON;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.elricyu.tt.action.base.AbstractAction;
import cn.elricyu.tt.bean.User;
import cn.elricyu.tt.bean.officer.MyOfficer;
import cn.elricyu.tt.bean.officer.Officer;
import cn.elricyu.tt.util.JsonUtil;

public class OfficerCheckAction extends AbstractAction<MyOfficer> {
	private static Log logger = LogFactory.getLog(OfficerCheckAction.class);
	private User self = null;

	public OfficerCheckAction(User self) {
		this.self = self;
		this.url = "root/officer.action";
	}

	public void process(JSON json) {
		MyOfficer myOfficer = self.getMyOfficer();
		if (myOfficer == null) {
			myOfficer = new MyOfficer();
			self.setMyOfficer(myOfficer);
		}
		doParseAll(myOfficer, json);
	}

	public static void doParseAll(MyOfficer myOfficer, JSON json) {
		try {
			int prestige = JsonUtil.getIntValue(json, "prestige", 0);
			int prestigetop = JsonUtil.getIntValue(json, "prestigetop", 0);
			long saveSalaryCD = JsonUtil.getLongValue(json, "savesalary_cd", 0);
			long contribejgCD = JsonUtil.getLongValue(json, "contribejg_cd", 0);
			int disJunGong = JsonUtil.getIntValue(json, "disjungong", 0);
			int disPres = JsonUtil.getIntValue(json, "dispres", 0);
			boolean canCont = JsonUtil.getBoolValue(json, "cancont", false);
			List<JSONObject> officerList = JsonUtil.getListJsonValue(json,
					"officerdto");
			myOfficer.setPrestige(prestige);
			myOfficer.setPrestigetop(prestigetop);
			myOfficer.setSaveSalaryCD(saveSalaryCD);
			myOfficer.setContribejgCD(contribejgCD);
			myOfficer.setDisJunGong(disJunGong);
			myOfficer.setDisPres(disPres);
			myOfficer.setCanCont(canCont);
			if (officerList == null || officerList.isEmpty()) {
				return;
			}
			{
				JSONObject currentJsonObject = officerList.get(0);
				Officer officer = myOfficer.getCurrentOfficer();
				if (officer == null) {
					officer = new Officer();
					myOfficer.setCurrentOfficer(officer);
				}
				int id = JsonUtil.getIntValue(currentJsonObject, "id", 0);
				int copper = JsonUtil.getIntValue(currentJsonObject, "copper",
						0);
				int rank = JsonUtil.getIntValue(currentJsonObject, "rank", 0);
				String name = JsonUtil.getStringValue(currentJsonObject,
						"name", "");
				int fame = JsonUtil.getIntValue(currentJsonObject, "fame", 0);
				String general = JsonUtil.getStringValue(currentJsonObject,
						"general", "");
				int att = JsonUtil.getIntValue(currentJsonObject, "att", 0);
				int def = JsonUtil.getIntValue(currentJsonObject, "def", 0);
				officer.setId(id);
				officer.setCopper(copper);
				officer.setRank(rank);
				officer.setName(name);
				officer.setFame(fame);
				officer.setGeneral(general);
				officer.setAtt(att);
				officer.setDef(def);
			}
			{
				if (officerList.size() < 2) {
					return;
				}
				JSONObject nextJsonObject = officerList.get(1);
				Officer officer = myOfficer.getNextOfficer();
				if (officer == null) {
					officer = new Officer();
					myOfficer.setNextOfficer(officer);
				}
				int id = JsonUtil.getIntValue(nextJsonObject, "id", 0);
				int copper = JsonUtil.getIntValue(nextJsonObject, "copper", 0);
				int rank = JsonUtil.getIntValue(nextJsonObject, "rank", 0);
				String name = JsonUtil.getStringValue(nextJsonObject, "name",
						"");
				int fame = JsonUtil.getIntValue(nextJsonObject, "fame", 0);
				String general = JsonUtil.getStringValue(nextJsonObject,
						"general", "");
				int att = JsonUtil.getIntValue(nextJsonObject, "att", 0);
				int def = JsonUtil.getIntValue(nextJsonObject, "def", 0);
				officer.setId(id);
				officer.setCopper(copper);
				officer.setRank(rank);
				officer.setName(name);
				officer.setFame(fame);
				officer.setGeneral(general);
				officer.setAtt(att);
				officer.setDef(def);
			}

		} catch (Exception e) {
		}
	}

}
